


LyLock_Banish = { }
table.insert(LyLock_Plugins, LyLock_Banish)
LyLock_Banish.Frame = "LyLockBanishFrame"
LyLock_Banish.Events = {
    "COMBAT_LOG_EVENT_UNFILTERED",
}


------------------------------------------------------
-- Banish Settings -----------------------------------
------------------------------------------------------

LyLock_Defaults.Banish_Message1 = "Banish is on %target%";
LyLock_Defaults.Banish_Message2 = "Banish is fading NOW";
LyLock_Defaults.Banish_Message3 = "Banish has faded EARLY";
LyLock_Defaults.Banish_Message4 = "FYI: %player% died while Banishing %target%!";

       
LyLock_Banish.OnSettings = function(action)
    if action=="default" then
	
		LyLock_Settings.Banish_Disable = ReturnSetting(LyLock_Settings.Banish_Disable, false)
        LyLock_Settings.Banish_MsgScreen = ReturnSetting(LyLock_Settings.Banish_MsgScreen, true)
        LyLock_Settings.Banish_Notify = ReturnSetting(LyLock_Settings.Banish_Notify, true)
		LyLock_Settings.Banish_Countdown = ReturnSetting(LyLock_Settings.Banish_Countdown, false)
		LyLock_Settings.Banish_Casting = ReturnSetting(LyLock_Settings.Banish_Casting, false)
		LyLock_Settings.Banish_Fading = ReturnSetting(LyLock_Settings.Banish_Fading, false)
		LyLock_Settings.Banish_Warlocks = ReturnSetting(LyLock_Settings.Banish_Warlocks, false)
        LyLock_Settings.Banish_DBMTimer = ReturnSetting(LyLock_Settings.Banish_DBMTimer, true)

		LyLock_Settings.Banish_Message1 = ReturnSetting(LyLock_Settings.Banish_Message1, LyLock_Defaults.Banish_Message1)
		LyLock_Settings.Banish_Message2 = ReturnSetting(LyLock_Settings.Banish_Message2, LyLock_Defaults.Banish_Message2)
		LyLock_Settings.Banish_Message3 = ReturnSetting(LyLock_Settings.Banish_Message3, LyLock_Defaults.Banish_Message3)
		LyLock_Settings.Banish_Message4 = ReturnSetting(LyLock_Settings.Banish_Message4, LyLock_Defaults.Banish_Message4)	
        
    elseif action=="save" then
    
        LyLock_Settings.Banish_Disable = iif(Banish_Disable:GetChecked(), true, false);
        LyLock_Settings.Banish_MsgScreen = iif(Banish_MsgScreen:GetChecked(), true, false);
        LyLock_Settings.Banish_Notify = iif(Banish_Notify:GetChecked(), true, false);
        LyLock_Settings.Banish_Countdown = iif(Banish_Countdown:GetChecked(), true, false);
        LyLock_Settings.Banish_Casting = iif(Banish_Casting:GetChecked(), true, false);
        LyLock_Settings.Banish_Fading = iif(Banish_Fading:GetChecked(), true, false);
        LyLock_Settings.Banish_Warlocks = iif(Banish_Warlocks:GetChecked(), true, false);
        LyLock_Settings.Banish_DBMTimer = iif(Banish_DBMTimer:GetChecked(), true, false);

        LyLock_Settings.Banish_Message1 = ReturnSetting(Banish_Message1:GetText(), LyLock_Defaults.Banish_Message1);
        LyLock_Settings.Banish_Message2 = ReturnSetting(Banish_Message2:GetText(), LyLock_Defaults.Banish_Message2);
        LyLock_Settings.Banish_Message3 = ReturnSetting(Banish_Message3:GetText(), LyLock_Defaults.Banish_Message3);
        LyLock_Settings.Banish_Message4 = ReturnSetting(Banish_Message4:GetText(), LyLock_Defaults.Banish_Message4);
    
    elseif action=="load" then

        Banish_Disable:SetChecked(LyLock_Settings.Banish_Disable);
        Banish_MsgScreen:SetChecked(LyLock_Settings.Banish_MsgScreen);
        Banish_Notify:SetChecked(LyLock_Settings.Banish_Notify);
        Banish_Countdown:SetChecked(LyLock_Settings.Banish_Countdown);
        Banish_Casting:SetChecked(LyLock_Settings.Banish_Casting);
        Banish_Fading:SetChecked(LyLock_Settings.Banish_Fading);
        Banish_Warlocks:SetChecked(LyLock_Settings.Banish_Warlocks);
        Banish_DBMTimer:SetChecked(LyLock_Settings.Banish_DBMTimer);

        Banish_Message1:SetText(LyLock_Settings.Banish_Message1);
        Banish_Message2:SetText(LyLock_Settings.Banish_Message2);
        Banish_Message3:SetText(LyLock_Settings.Banish_Message3);
        Banish_Message4:SetText(LyLock_Settings.Banish_Message4);    
        
    end
end



--------------------------------------------------------------------------------------
-- Banish Functions ------------------------------------------------------------------
--------------------------------------------------------------------------------------

LyLock_Banish.Message=0
LyLock_Banish.Hostile=nil
LyLock_Banish.HostileID=nil
LyLock_Banish.Started=0
LyLock_Banish.Broke=false
LyLock_Banish.Rank=nil
LyLock_Banish.Timeout=0

LyLock_Banish.ShowTimer = function()       

    if (DBM) then
        LyLock_Banish.DMBTimer = {}
        LyLock_Banish.DMBTimer.BanishTimerText = UnitName("player").."'s Banish"
        LyLock_Banish.DMBTimer.BanishTimerMin = 0
        LyLock_Banish.DMBTimer.BanishTimerSec =LyLock_Banish.Timeout
        if( DBM.Rank > 0 ) then
        else
            --LyLock_Banish.DMBTimer.BanishTimerBroadcast = false;
        end

        DBM.StartStatusBarTimer(
            ((LyLock_Banish.DMBTimer.BanishTimerMin * 60 ) +
              LyLock_Banish.DMBTimer.BanishTimerSec),
            LyLock_Banish.DMBTimer.BanishTimerText, 
            not LyLock_Banish.DMBTimer.BanishTimerBroadcast
        )
        if ((LyLock_Banish.DMBTimer.BanishTimerMin * 60 ) + LyLock_Banish.DMBTimer.BanishTimerSec) > 10 then 
            if LyLock_Banish.DMBTimer.BanishTimerBroadcast then
                DBM.Schedule(((LyLock_Banish.DMBTimer.BanishTimerMin * 60 ) + LyLock_Banish.DMBTimer.BanishTimerSec) - 10, "DBM.Announce", string.format(DBM_TIMER_IS_ABOUT_TO_EXPIRE, LyLock_Banish.DMBTimer.BanishTimerText));
            else
                DBM.Schedule(((LyLock_Banish.DMBTimer.BanishTimerMin * 60 ) + LyLock_Banish.DMBTimer.BanishTimerSec) - 10, "DBM.AddSpecialWarning", string.format(DBM_TIMER_IS_ABOUT_TO_EXPIRE, LyLock_Banish.DMBTimer.BanishTimerText));
            end
        end    
    
    end
end

LyLock_Banish.OnEvent = function()
      
	if event=="COMBAT_LOG_EVENT_UNFILTERED" then

--[[ 
	 -- filter only player events in log
	 if arg4 == UnitName("player") or string.find(arg2, "SPELL_AURA") ~= nil then
	 
	    msg = "CLEU ";
	    msg = msg .. " arg1["
	    if (arg1 ~= nill) then
	    	msg = msg .. arg1 .. "]"
	    else
	    	msg = msg .. "<<nil>>]"
	    end
	    msg = msg .. " arg2["
	    if (arg2 ~= nill) then
	    	msg = msg .. arg2 .. "]"
	    else
	    	msg = msg .. "<<nil>>]"
	    end
	    msg = msg .. " arg3["
	    if (arg3 ~= nill) then
	    	msg = msg .. arg3 .. "]"
	    else
	    	msg = msg .. "<<nil>>]"
	    end
	EchoMessage("LyLockTest", msg, "Debug") 
	    msg = "CLEU ";
	    msg = msg .. " arg4["
	    if (arg4 ~= nill) then
	    	msg = msg .. arg4 .. "]"
	    else
	    	msg = msg .. "<<nil>>]"
	    end
	    msg = msg .. " arg5["
	    if (arg5 ~= nill) then
	    	msg = msg .. arg5 .. "]"
	    else
	    	msg = msg .. "<<nil>>]"
	    end
	    msg = msg .. " arg6["
	    if (arg6 ~= nill) then
	    	msg = msg .. arg6 .. "]"
	    else
	    	msg = msg .. "<<nil>>]"
	    end
	EchoMessage("LyLockTest", msg, "Debug")
	    msg = "CLEU ";
	    msg = msg .. " arg7["
	    if (arg7~= nill) then
	    	msg = msg .. arg7 .. "]"
	    else
	    	msg = msg .. "<<nil>>]"
	    end
	    msg = msg .. " arg8["
	    if (arg8 ~= nill) then
	    	msg = msg .. arg8 .. "]"
	    else
	    	msg = msg .. "<<nil>>]"
	    end
	EchoMessage("LyLockTest", msg, "Debug")
	    msg = "CLEU ";
	    msg = msg .. " arg9["
	    if (arg9 ~= nill) then
	    	msg = msg .. arg9 .. "]"
	    else
	    	msg = msg .. "<<nil>>]"
	    end
	    msg = msg .. " arg10["
	    if (arg10 ~= nill) then
	    	msg = msg .. arg10 .. "]"
	    else
	    	msg = msg .. "<<nil>>]"
	    end
	    msg = msg .. " arg11["
	    if (arg11 ~= nill) then
	    	msg = msg .. arg11 .. "]"
	    else
	    	msg = msg .. "<<nil>>]"
	    end
	EchoMessage("LyLockTest", msg, "Debug")
		if HostileID ~= nil then
			EchoMessage("LyLockTest", "HostileID [" .. LyLock_Banish.HostileID .. "]", "Debug")
		end
	end
]]--

		if arg2 == "SPELL_AURA_APPLIED" and arg10 == WORD_BANISH and arg6 == LyLock_Banish.HostileID then 
            LyLock_Banish.Started = GetTime()
            LyLock_Banish.Broke = false
            
            LyLock_Banish.ShowTimer()
            
            if not LyLock_Settings.Banish_Disable then
                if LyLock_Settings.Banish_Casting then
                    Speak(LyMsg(LyLock_Settings.Banish_Message1, LyLock_Banish.Hostile))
                else
                    EchoMessage("LyLock", LyMsg(LyLock_Settings.Banish_Message1, LyLock_Banish.Hostile), "Casting")
                end
            end
--[[  have to find examples of resists; immunes will never cast
	        elseif (string.find(arg1, LYLOCK_RESISTED) or string.find(arg1, LYLOCK_IMMUNE)) then
	            LyLock_Banish.Hostile = nil
	            LyLock_Banish.HosstileID = nil
	            LyLock_Banish.Started = 0
	            LyLock_Banish.Broke = false
	            EchoMessage("LyLock", "Banish resisted or target is immune", "Canceled")
	        end
]]--
	   elseif arg2 == "SPELL_AURA_REMOVED" and arg10 == WORD_BANISH and arg6 == LyLock_Banish.HostileID then -- check if banish broke early
	   	  local banishElapse = (GetTime() - LyLock_Banish.Started)
	   	  if ((banishElapse <=28 ) and (LyLock_Banish.Rank == 2)) or ((banishElapse <= 18) and (LyLock_Banish.Rank ==1 )) then
   	        LyLock_Banish.Broke = true
            LyLock_Banish.Started = 0
            if not LyLock_Settings.Banish_Disable then
                if LyLock_Settings.Banish_MsgScreen then
                    ScreenMessage(LyMsg(LyLock_Settings.Banish_Message3, LyLock_Banish.Hostile))
                end
                if LyLock_Settings.Banish_Fading then
                    Speak(LyMsg(LyLock_Settings.Banish_Message3, LyLock_Banish.Hostile))
                else
                    EchoMessage("LyLock", LyMsg(LyLock_Settings.Banish_Message3, LyLock_Banish.Hostile), "Fading")
                end
            end
            LyLock_Banish.Hostile = nil
            LyLock_Banish.HostileID = nil
            
            if (DBM) then
            	-- kill any open timers
            	DBM.EndStatusBarTimer(LyLock_Banish.DMBTimer.BanishTimerText)
            end
          end	   	  
	   elseif arg2 =="PLAYER_DEAD" then
	        if not LyLock_Settings.Banish_Disable then
	            if LyLock_Settings.Banish_Warlocks then
	                if (UnitIsDeadOrGhost("player")==1) then
	                    if (LyLock_Banish.Started>0) and (LyLock_Banish.Hostile~=nil) then
	                        WhisperWarlocks(LyMsg(LyLock_Settings.Banish_Message4, LyLock_Banish.Hostile)) --casted a banish
	                    end
	                end
	            end
	        end
	    elseif arg2 =="SPELL_CAST_START" then	
	        if  arg4==UnitName("player") and arg10==WORD_BANISH then
	            if arg9 == 710 then -- spell ID for BANISH RANK 1
	                LyLock_Banish.Timeout = 19.5
	                LyLock_Banish.Rank = 1
	            elseif arg9 == 18647 then -- spell ID for BANISH RANK 2
	                LyLock_Banish.Timeout = 29.5
	                LyLock_Banish.Rank = 2
	            end
	
	            LyLock_Banish.Message=0
	            LyLock_Banish.Broke = false
	            LyLock_Banish.Hostile = UnitName("target") -- for banish we can't use arg7
	            LyLock_Banish.HostileID = UnitGUID("target") -- for banish we can't use arg6
	            
--	            EchoMessage("LyLock", "Detect Banish on " .. LyLock_Banish.Hostile .. "[" .. LyLock_Banish.HostileID .. "]", "Debug")
	        end
	    elseif arg2 =="SPELL_CAST_INTERRUPTED" or arg2 =="SPELL_CAST_FAILED" then
	    	-- Interrupted casting banish?
	    	if arg4 == UnitName("player") and (arg9 == 710 or arg9 == 18647) then
	            LyLock_Banish.Hostile = nil
	            LyLock_Banish.HostileID = nil
	        end
	    end
	end
end




LyLock_Banish.OnSecond = function()

	if (LyLock_Banish.Started>0) and not LyLock_Banish.Broke then
		local banishElapse = (GetTime() - LyLock_Banish.Started)
		        
		if not LyLock_Settings.Banish_Disable then
			if (((banishElapse>=14) and (LyLock_Banish.Rank==2)) or ((banishElapse>=4) and (LyLock_Banish.Rank==1))) and LyLock_Banish.Message==0 then
                LyLock_Banish.Message = 1
                if LyLock_Settings.Banish_Notify then
                    if LyLock_Settings.Banish_Countdown then
                        Speak(LyMsg("Banish will fade in 15 Sec", LyLock_Banish.Hostile))
                    else
                        EchoMessage("LyLock", "Banish will fade in 15 Sec", "Countdown")
                    end
                end
			elseif (((banishElapse>=19) and (LyLock_Banish.Rank==2)) or ((banishElapse>=9) and (LyLock_Banish.Rank==1))) and LyLock_Banish.Message==1 then
                LyLock_Banish.Message = 2
                if LyLock_Settings.Banish_Notify then
                    if LyLock_Settings.Banish_MsgScreen then
                        ScreenMessage(LyMsg("Banish will fade in 10 Sec", LyLock_Banish.Hostile))
                    end
                    if LyLock_Settings.Banish_Countdown then
                        Speak(LyMsg("Banish will fade in 10 Sec", LyLock_Banish.Hostile))
                    else
                        EchoMessage("LyLock", "Banish will fade in 10 Sec", "Countdown")
                    end
                end
			elseif (((banishElapse>=24) and (LyLock_Banish.Rank==2)) or ((banishElapse>=14) and (LyLock_Banish.Rank==1))) and LyLock_Banish.Message==2 then
                LyLock_Banish.Message = 3
                if LyLock_Settings.Banish_Notify then
                    if LyLock_Settings.Banish_Countdown then
                        Speak(LyMsg("Banish will fade in 5 Sec", LyLock_Banish.Hostile))
                    else
                        EchoMessage("LyLock", "Banish will fade in 5 Sec", "Countdown")
                    end
                end
			end
		end
		
		if ((banishElapse>=28) and (LyLock_Banish.Rank== 2)) or ((banishElapse>=18) and (LyLock_Banish.Rank==1)) then
			LyLock_Banish.Broke = true
			LyLock_Banish.Started = 0
			if not LyLock_Settings.Banish_Disable then
                if LyLock_Settings.Banish_MsgScreen then
                    ScreenMessage(LyMsg(LyLock_Settings.Banish_Message2, LyLock_Banish.Hostile))
                end
				if LyLock_Settings.Banish_Fading then
					Speak(LyMsg(LyLock_Settings.Banish_Message2, LyLock_Banish.Hostile))
				else
					EchoMessage("LyLock", LyMsg(LyLock_Settings.Banish_Message2, LyLock_Banish.Hostile), "Fading")
				end
			end
			LyLock_Banish.Hostile = nil
			LyLock_Banish.HostileID = nil;
		end
	end
end



